package chapter06

/**
  *
  * @author Pop
  * @date 2022/6/20 22:00
  */
object Test11_Object {
  def main(args: Array[String]): Unit = {
//      val s:Student11 = new Student11("11",12)
      val s:Student11 = Student11.newStudent("11",12)

      // 默认调用伴生对象的apply对应方法
//      val s1:Student11 = new Student11("22",23,"22")

      s.printInfo()
  }
}

// 柱构造器私有化
class Student11 private(val name:String,val age:Int){
  def printInfo(): Unit ={
    println(s"student:name = ${name},age = $age,school = ${Student11.school}")
  }
}
// 公共的 定义在这里，单例对象中，伴生类中可以访问
object Student11{
  val school:String = "pop"

  //  定义一个类的对象实例的构造方法
  def newStudent(name:String,age:Int): Student11 ={
    new Student11(name,age)
  }

  /**
    * 如果你伴生对象定义了apply，在new 对应的伴生类的时候，会寻找对应的
    * apply符合的形参，帮助初始化
    * @param name
    * @param age
    * @return
    */
  def apply(name: String,age: Int,sex:String): Student11 = new Student11(name,age)
}


